<?php

namespace app\api\model;

use app\common\exception\BaseException;
use app\common\model\BaseModel;

/**
 * 服务队
 * Class Team
 * @package app\api\model
 */
class Team extends BaseModel
{
    /**
     * 追加字段
     * @var array
     */
    protected $append = [
    ];

    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'is_delete',
        'wxapp_id'
    ];

    /**
     * 服务队介绍：HTML实体转换回普通字符
     * @param $value
     * @return string
     */
    public function getTeamContentAttr($value)
    {
        return htmlspecialchars_decode($value);
    }

    public function getViewTimeAttr($value, $data)
    {
        return date('Y-m-d', $data['create_time']);
    }

    /**
     * 关联会员人数
     */
    public function getTeamPeopleAttr($value)
    {
        switch ($value) {
            case "1":
                return "0-25人";
                break;
            case "2":
                return "25-50人";
                break;
            case "3":
                return "50-100人";
                break;
            case "4":
                return "100人以上";
                break;
        }

    }

    /**
     * 服务队详情
     * @param $team_id
     * @throws BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public static function detail($team_id)
    {

        $model = self::with(['image', 'category', 'images.file', 'banner', 'header'])->where("team_id", '=', $team_id)->find();
        if (!$model) {
            throw new BaseException(['msg' => '服务队不存在']);
        }
        return $model;
    }

    /**
     * 获取服务队列表
     * @param int $category_id
     * @param int $limit
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($category_id = 0, $searchInput = "", $limit = 10000, $team_id = 0, $shanghui_id = 0)
    {
        $a = self::with(['image', 'category'])
            ->where('team_status', '=', 1)
            ->where('is_delete', '=', 0)
            ->where('team_id', '=', $team_id);
        if ($category_id) {
            $a->where('category_id', '=', $category_id);
        }
        if ($searchInput != "") {
            $a->where('team_name', 'like', '%' . $searchInput . '%');
        }
        $a = $a->find();
        if ($a) {
            $a = $a->toArray();
        }
        $category_id > 0 && $this->where('category_id', '=', $category_id);
        $searchInput != "" && $this->where('team_name', 'like', '%' . $searchInput . '%');
        $b = $this->with(['image', 'category'])
            ->where('team_status', '=', 1)
            ->where('is_delete', '=', 0)
            ->where('team_id', '<>', $team_id)
            ->where('shanghui_id', '=', $shanghui_id)
            ->order(['team_sort' => 'asc', 'create_time' => 'desc'])
            ->paginate($limit, false, [
                'query' => \request()->request()
            ]);
        if ($b) {
            $b = $b->toArray();
        }
        if ($a) {
            array_unshift($b['data'], $a);
        }
        return $b;
    }

    /**
     * 关联封面图
     * @return \think\model\relation\HasOne
     */
    public function image()
    {
        return $this->hasOne('uploadFile', 'file_id', 'image_id');
    }

    /**
     * 关联分类表
     * @return \think\model\relation\BelongsTo
     */
    public function category()
    {
        return $this->BelongsTo("TeamCategory", 'category_id', 'category_id');
    }

    /**
     * 关联服务队图片表
     * @return \think\model\relation\HasMany
     */
    public function images()
    {
        return $this->hasMany("app\\common\\model\\TeamImage")->order(['id' => 'asc']);
    }

    /**
     * 关联服务队banner图
     * @return \think\model\relation\HasOne
     */
    public function banner()
    {
        return $this->hasOne('uploadFile', 'file_id', 'banner_id');
    }

    /**
     * 关联服务队banner图
     * @return \think\model\relation\HasOne
     */
    public function header()
    {
        return $this->hasOne('uploadFile', 'file_id', 'header_id');
    }

}